package com.o.Reflection;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 *
 *
 * @author Mc.Chai
 * @program: Java基础
 * @create 2021-11-29-21:15 21:15
 */
public class TestClassMethods {
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> dog = Class.forName("com.o.Reflection.Dog");
        // 1.String    getName------->获得此类名
        System.out.println("类名:"+dog.getName());
        // 2.Package   getPackage---->获得此类的包
        System.out.println("包名:"+dog.getPackage().getName());
        // 3.Class<>       getSuperclass--->获得父类的类对象
        System.out.println("dog.getSuperclass() = " + dog.getSuperclass().getName());
        // 4.Class<>[]     		 getInterfaces---->获得接口
        Class<?>[] interfaces = dog.getInterfaces();
        for (Class interface1 :
                interfaces) {
            System.out.println("接口:"+interface1.getName());
        }
        // 5.Field[]	getFields---------->获得所有公开属性（+父类公开属性）的Field对象
        Field[] fields = dog.getFields();
        for (Field field :
                fields) {
            System.out.println("getFields属性:"+field.getName());
        }
        // 6.Field[]	getDeclaredFields---->获得所有自己的属性（不含有父类属性）
        Field[] declaredFields = dog.getDeclaredFields();
        for (Field field :
                declaredFields) {
            System.out.println("getDeclaredFields属性"+field.getName());
        }
        // 7.Method[]	getMethods----->获得所有公开（+父类公开方法）方法,包含Object类
        Method[] methods = dog.getMethods();
        for (Method method :
                methods) {
            System.out.println("method = " + method.getName());
        }
        // 8.Method[]		getDeclaredMethods---->获得所有的方法（不含父类）
        Method[] declaredMethods = dog.getDeclaredMethods();
        for (Method method :
                declaredMethods) {
            System.out.println("不含有父类method = " + method.getName());
        }
        // 9.Constructor<>[]	getConstructors----->获取所有构造方法
        Constructor<?>[] constructors = dog.getConstructors();
        for (Constructor constructor :
                constructors) {
            System.out.println("constructor = " + constructor.getName());

        }
    }
}
class Animal{
    public String name;
    public Animal(){

    }
    public void eat(){    }
}
class Dog extends Animal implements Serializable {
    public String color;
    Integer age;
    public Dog(){
        super();
    }
    public void run(){};

}
